﻿using System.Collections.Generic;
using System.Data.Linq;

namespace Devamigo.Core
{
    public class LinqToSqlRepository : IRepository
    {
        private DataContext _dataContext = null;

        public LinqToSqlRepository(DataContext dataContext)
        {
            _dataContext = dataContext;
        }

        #region IRepository Members

        public IEnumerable<TModel> Query<TModel>() 
            where TModel : class
        {
            return _dataContext.GetTable<TModel>();
        }

        public void InsertOnSubmit<TModel>(TModel entity) 
            where TModel : class
        {
            _dataContext.GetTable<TModel>().InsertOnSubmit(entity);
        }

        public void InsertOnSubmit<TModel>(IEnumerable<TModel> entities) 
            where TModel : class
        {
            _dataContext.GetTable<TModel>().InsertAllOnSubmit(entities);
        }

        public void DeleteOnSubmit<TModel>(TModel entity) 
            where TModel : class
        {
            _dataContext.GetTable<TModel>().DeleteOnSubmit(entity);
        }

        public void DeleteOnSubmit<TModel>(IEnumerable<TModel> entities) 
            where TModel : class
        {
            _dataContext.GetTable<TModel>().DeleteAllOnSubmit(entities);
        }

        public void SubmitChanges()
        {
            _dataContext.SubmitChanges();
        }

        #endregion
    }
}
